// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        tiledMap: cc.TiledMap,

        dialogNode : cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let p = cc.director.getPhysicsManager();   //物理引擎
        p.enabled = true;   
        // p.debugDrawFlags =  true;
        p.gravity = cc.v2(0, 0);  //关闭重力
    },


    //初始化地图
    initMap(){

        let tiledSize = this.tiledMap.getTileSize();

        let layer = this.tiledMap.getLayer("wall");   //获取墙体的图层
        let layerSize = layer.getLayerSize();    /// 获取墙体图层的size对象 ： size对象是一个包含width, height的对象

        for(let i=0; i<layerSize.width; i++){
            for(let j=0; j<layerSize.height; j++){
                let tiled = layer.getTiledTileAt(i,j,true);  // 从指定图层获取元素块
                if(tiled.gid != 0){  //判断该图快是否存在
                    tiled.node.group = "wall"; // 设置图块的碰撞组为wall
                    
                    let body = tiled.node.addComponent(cc.RigidBody);  //给图块的元素组件添加 刚体组件
                    body.type = cc.RigidBodyType.Static;   //  设置图块的刚体类型为静态类型， 防止受重力影响掉下去
                    
                    let collider = tiled.node.addComponent(cc.PhysicsBoxCollider);  // 给图块元素添加物理引擎组件
                    collider.offset = cc.v2(tiledSize.width /2 , tiledSize.height/ 2); // 给图块设置偏移量 为 图的中心点
                    collider.size = tiledSize;  //包围盒的大小设为 地图的大小；
                    collider.apply();  //激活物理引擎;
                }
            }
        }

        // this.dialog = this.dialogNode.getComponent("dialog");
        // this.dialog.init([
        //     {roleId: 1, content: "我是勇者"},
        //     {roleId: 2, content: "我是魔王"},
        //     {roleId: 1, content: "好啊好啊哈"},
        //     {roleId: 2, content: "我是魔王胜多负少发射点发射点JFK受到激发教师的"},
        //     {roleId: 2, content: "我士大夫十分健身房速度反馈是否是的开发接口十分士大夫可是大家"},
        //     {roleId: 2, content: "我士大夫十分健身房速度反馈是否是的开发接口十分士大夫可是大家士大夫十分健身房速度反馈是否是的开发接口十分士大夫可是大家王"},
        // ]);
        

    },

    start () {
        this.initMap();

        
    },

    // update (dt) {},
});
